Skip to content

Add tenant query filtering in TenantInterceptor via OnQuery - #95

Merged
nnhy merged 3 commits into
masterfrom
copilot/add-tenant-interceptor-query-filter
Aug 27, 2026
Merged

Add tenant query filtering in TenantInterceptor via OnQuery#95
nnhy merged 3 commits into
masterfrom
copilot/add-tenant-interceptor-query-filter

Conversation

Copilot AI commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

TenantInterceptor only handled create/validate flows, so tenant-scoped entities were not consistently filtered during query construction. This change adds query-time tenant condition injection at the interceptor layer so tenant filtering is applied automatically for registered entities implementing ITenantScope.

  • Interceptor query-path fix

    • Added OnQuery(IEntityFactory, Expression?, QueryAction) to TenantInterceptor.
    • Normalizes incoming where (including null) and appends tenant constraints through existing ApplyTenant(factory) logic.
    • Keeps behavior centralized in interceptor pipeline instead of relying on manual ApplyTenant calls in business code.
  • Focused coverage for tenant query behavior

    • Added tests in TenantContextTests for:
      • auto-adding tenant filter when tenant context exists,
      • merging tenant filter with existing query predicates,
      • no-op when tenant context is absent.
protected override XCode.Expression? OnQuery(IEntityFactory factory, XCode.Expression? where, QueryAction action)
{
    return (where & XCode.Expression.Empty).ApplyTenant(factory);
}

Copilot AI linked an issue Aug 14, 2026 that may be closed by this pull request
Co-authored-by: nygula <13066114+nygula@users.noreply.github.com>
@nygula

nygula commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

须加测如下场景 : 1.有租户 + 已有whereexpression的情况 是否自动过滤 2. 有租户.纯FindAll() Find()等无whereexpression的情况 是否空查询也过滤 3. tennatid=0的情况 是否可见全部 4. 未设置租户上下文的情况 是否会报错 @copilot

Copilot AI changed the title [WIP] Add query filter to TenantInterceptor Add tenant query filtering in TenantInterceptor via OnQuery Aug 14, 2026
Copilot AI requested a review from nygula August 14, 2026 03:54
Co-authored-by: nygula <13066114+nygula@users.noreply.github.com>

Copilot AI commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

须加测如下场景 : 1.有租户 + 已有whereexpression的情况 是否自动过滤 2. 有租户.纯FindAll() Find()等无whereexpression的情况 是否空查询也过滤 3. tennatid=0的情况 ...

已补充你提到的四类场景测试并提交到 45a634a

  1. 有租户 + 已有 WhereExpression 自动合并租户条件;
  2. 有租户 + FindAll()/Find() 空条件查询自动加租户过滤;
  3. TenantId=0 时不过滤(可见全部);
  4. 未设置租户上下文时不报错。

@nnhy
nnhy marked this pull request as ready for review August 27, 2026 15:17
Copilot AI lite review requested due to automatic review settings August 27, 2026 15:17
@nnhy
nnhy merged commit a4a428f into master Aug 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds query-time tenant filtering to the XCode interceptor pipeline so entities implementing ITenantScope automatically get a TenantId constraint injected during query construction, and extends the existing tenant test suite to cover this query behavior.

Changes:

  • Added TenantInterceptor.OnQuery(IEntityFactory, Expression?, QueryAction) that normalizes/merges predicates and applies ApplyTenant(factory).
  • Added unit tests validating tenant filter injection, predicate merging, and no-op behavior when tenant context is absent / TenantId == 0.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
XCode/Membership/ITenantSource.cs Adds TenantInterceptor.OnQuery to inject tenant filtering into query predicates.
XUnitTest.XCode/Membership/TenantContextTests.cs Adds tests covering tenant query filtering behavior through the interceptor.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +198 to 202
protected override XCode.Expression? OnQuery(IEntityFactory factory, XCode.Expression? where, QueryAction action)
{
return (where & XCode.Expression.Empty).ApplyTenant(factory);
}
}
Comment on lines +198 to +201
protected override XCode.Expression? OnQuery(IEntityFactory factory, XCode.Expression? where, QueryAction action)
{
return (where & XCode.Expression.Empty).ApplyTenant(factory);
}
Comment on lines +364 to +374
[Fact]
[DisplayName("OnQuery_有租户且已有WhereExpression时合并租户条件")]
public void TenantModule_OnQuery_WithTenantAndWhereExpression_MergesTenantFilter()
{
// Arrange
var module = new TenantInterceptor();
TenantContext.Current = new TenantContext { TenantId = 456 };
var factory = TenantTestEntity.Meta.Factory;
var where = new WhereExpression();
where &= TenantTestEntity._.Name == "Stone";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

租户拦截过滤器缺少查询相关

4 participants